package com.wzh.yebServe.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzh.yebServe.mapper.DepartmentMapper;
import com.wzh.yebServe.pojo.Department;
import com.wzh.yebServe.pojo.ResultBean;
import com.wzh.yebServe.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author wzh
 * @create 2022/11/17 19:59
 * @Description:
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> getAllDepartments() {
        return departmentMapper.getAllDepartmens(-1);
    }

    @Override
    public ResultBean addDep(Department department) {
        department.setEnabled(true);
        departmentMapper.addDep(department);
        Integer result = department.getResult();
        if (result==1){
            return ResultBean.success("添加成功",department);
        }
        return ResultBean.error("添加失败");
    }

    @Override
    public ResultBean deleteDepartment(Integer id) {
        Department dep=new Department();
        dep.setId(id);
        departmentMapper.deleteDepartment(dep);
        if (dep.getResult()==-2){
            return ResultBean.error("该部门下还有子部门，删除失败");
        }
        if (dep.getResult()==-1){
            return ResultBean.error("该部门下还有员工，删除失败");
        }
        if (dep.getResult()==1){
            return ResultBean.success("删除成功");
        }
        return ResultBean.error("删除失败");
    }
}
